package com.example.opengldemo._08_3D.column;

import android.opengl.GLES30;
import android.util.Log;
import android.view.View;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;
import com.example.opengldemo.util.ShapeFactory;

public class ColumnL {
    String vertexShaderPath = "d3d/vertex.sh";
    String fragShaderPath = "d3d/fragment.sh";

    public ColumnL(View view) {
        initProgram(view);
        initBuffer();
    }

    private void initProgram(View view) {
        String vertexShaderRes = ShaderUtil.loadFromAssetsFile(vertexShaderPath, view.getResources());
        String fragShaderRes = ShaderUtil.loadFromAssetsFile(fragShaderPath, view.getResources());
        initHandle(ShaderUtil.createProgram(vertexShaderRes, fragShaderRes));
    }

    private void initHandle(int mProgram) {
        shaderHandle = new ShaderHandle(mProgram).build();
    }

    public ShaderHandle shaderHandle;
    public ShaderBuffer shaderBuffer;

    private void initBuffer() {
        shaderBuffer = new ShaderBuffer();
        shaderBuffer.setVertexBuffer(getVertexArray());
        shaderBuffer.setTexcoorBuffer(getTexcoorArray());
        shaderBuffer.setColorBuffer(getColorArray());

    }
    int span = 10;
    float h = 0.9f ;
    public float[] getVertexArray() {
        int count = 360 / span +1  ;
        float r = 0.8f;
        float[] vertex = new float[count * 2 * 3];

        for (int i = 0 ; i < count; i++) {
            int spanTemp = (i) * span;
            vertex[i * 6 + 0] = (float) (r * Math.cos(Math.toRadians(spanTemp)));
            vertex[i * 6 + 1] =  (float) (r * Math.sin(Math.toRadians(spanTemp)));
            vertex[i * 6 + 2] = 0f ;
            vertex[i * 6 + 3] = (float) (r * Math.cos(Math.toRadians(spanTemp)));
            vertex[i * 6 + 4] =  (float) (r * Math.sin(Math.toRadians(spanTemp)));
            vertex[i * 6 + 5] = h ;
        }
        shaderBuffer.vCount = vertex.length/ 3 ;
        return vertex;
    }

    public float[] getTexcoorArray() {
        float a = 1.0f/37 ;
        float[] texcoors = new float[shaderBuffer.vCount * 2] ;
        for (int i = 0 ; i < 37; i++) {
            int spanTemp = (i) * span;
            texcoors[i * 4 + 0] = i*a ;
            texcoors[i * 4 + 1] = 0 ;

            texcoors[i * 4 + 2] = i*a  ;
            texcoors[i * 4 + 3] = 1.0f;
        }
        return texcoors;
    }

    public float[] getNormalArray() {
        return null;
    }

    public float[] getColorArray(){
        return ShapeFactory.getFragDataArray(shaderBuffer.vCount);
    }

    public void drawSelf(int textureid , int textureid2 , int textureid3) {
        GLES30.glUseProgram(shaderHandle.getProgram());
        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_PositionHandle(),
                3,
                GLES30.GL_FLOAT,
                false,
                3 * 4,
                shaderBuffer.getVertexBuffer()
        );
        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_ColorHandle(),
                4,
                GLES30.GL_FLOAT,
                false,
                4 * 4,
                shaderBuffer.getColorBuffer()
        );

        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_TexCoorsHandle(),
                2,
                GLES30.GL_FLOAT,
                false,
                2 * 4,
                shaderBuffer.getTexcoorBuffer()
        );

        GLES30.glUniformMatrix4fv(
                shaderHandle.get_U_MVPMatrixHandle(),
                1,
                false,
                shaderBuffer.getMVPMatrixArray(),
                0
        );

        GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , textureid);

        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_PositionHandle());
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_ColorHandle());
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_TexCoorsHandle());

        GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0 ,  shaderBuffer.vCount);
    }


}
